{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"A very simple MNIST classifier.\n",
    "See extensive documentation at\n",
    "https://www.tensorflow.org/get_started/mnist/beginners\n",
    "\"\"\"\n",
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "FLAGS = None\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们在这里调用系统提供的Mnist数据函数为我们读入数据，如果没有下载的话则进行下载。\n",
    "\n",
    "<font color=#ff0000>**这里将data_dir改为适合你的运行环境的目录**</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting mnist\\train-images-idx3-ubyte.gz\n",
      "Extracting mnist\\train-labels-idx1-ubyte.gz\n",
      "Extracting mnist\\t10k-images-idx3-ubyte.gz\n",
      "Extracting mnist\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = 'mnist'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个非常非常简陋的模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the model\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "#添加一层神经元\n",
    "W1 = tf.Variable(tf.truncated_normal([784, 1024],stddev=0.1))\n",
    "b1 = tf.Variable(tf.truncated_normal([1024],stddev=0.1))\n",
    "\n",
    "W2 = tf.Variable(tf.truncated_normal([1024, 10],stddev=0.1))\n",
    "b2 = tf.Variable(tf.truncated_normal([10],stddev=0.1))\n",
    "\n",
    "#W3 = tf.Variable(tf.truncated_normal([100, 60],stddev=0.1))\n",
    "#b3 = tf.Variable(tf.zeros([60]))\n",
    "\n",
    "#W4 = tf.Variable(tf.truncated_normal([60, 10],stddev=0.1))\n",
    "#b4 = tf.Variable(tf.zeros([10]))\n",
    "\n",
    "logits1 = tf.matmul(x, W1) + b1\n",
    "#logits2 = tf.matmul(tf.nn.elu(logits1), W2) + b2\n",
    "#logits3 = tf.matmul(tf.nn.elu(logits2), W3) + b3\n",
    "\n",
    "y =tf.matmul( tf.nn.elu(logits1), W2)+b2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义我们的ground truth 占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define loss and optimizer\n",
    "y_ = tf.placeholder(tf.float32, [None, 10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来我们计算交叉熵，注意这里不要使用注释中的手动计算方式，而是使用系统函数。\n",
    "另一个注意点就是，softmax_cross_entropy_with_logits的logits参数是**未经激活的wx+b**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The raw formulation of cross-entropy,\n",
    "#\n",
    "#   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),\n",
    "#                                 reduction_indices=[1]))\n",
    "#\n",
    "# can be numerically unstable.\n",
    "#\n",
    "# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw\n",
    "# outputs of 'y', and then average across the batch.\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
    "#增加正则项\n",
    "regularizerW1=tf.contrib.layers.l2_regularizer(0.4)(W1)\n",
    "regularizerW2=tf.contrib.layers.l2_regularizer(0.4)(W2)\n",
    "#regularizerW3=tf.contrib.layers.l2_regularizer(0.4)(W3)\n",
    "#regularizerW4=tf.contrib.layers.l2_regularizer(0.4)(W4)\n",
    "\n",
    "#regularizerB1=tf.contrib.layers.l2_regularizer(0.1)(b1)\n",
    "#regularizerB2=tf.contrib.layers.l2_regularizer(0.005)(b2)\n",
    "\n",
    "regularizer= regularizerW1+regularizerW2\n",
    "\n",
    "loss = regularizer+cross_entropy\n",
    "loss = cross_entropy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成一个训练step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)\n",
    "train_step = tf.train.AdamOptimizer(0.01).minimize(loss)#优化学习率\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这里我们仍然调用系统提供的读取数据，为我们取得一个batch。\n",
    "然后我们运行3k个step(5 epochs)，对权重进行优化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2557\n",
      "0.8326\n",
      "0.8519\n",
      "0.8996\n",
      "0.9112\n",
      "0.9206\n",
      "0.9412\n",
      "0.9455\n",
      "0.9374\n",
      "0.898\n",
      "0.948\n",
      "0.9293\n",
      "0.9227\n",
      "0.9292\n",
      "0.9481\n",
      "0.9424\n",
      "0.9212\n",
      "0.9506\n",
      "0.9418\n",
      "0.9372\n",
      "0.934\n",
      "0.9303\n",
      "0.9117\n",
      "0.9434\n",
      "0.9448\n",
      "0.9395\n",
      "0.9292\n",
      "0.9524\n",
      "0.9461\n",
      "0.9577\n",
      "0.9222\n",
      "0.9518\n",
      "0.9608\n",
      "0.9399\n",
      "0.9491\n",
      "0.9625\n",
      "0.9394\n",
      "0.953\n",
      "0.9564\n",
      "0.9602\n",
      "0.9448\n",
      "0.9448\n",
      "0.9525\n",
      "0.9548\n",
      "0.9399\n",
      "0.9524\n",
      "0.9648\n",
      "0.9365\n",
      "0.9618\n",
      "0.9548\n",
      "0.9624\n",
      "0.9619\n",
      "0.9641\n",
      "0.9669\n",
      "0.9628\n",
      "0.9532\n",
      "0.9558\n",
      "0.9436\n",
      "0.9583\n",
      "0.9404\n",
      "0.9314\n",
      "0.9606\n",
      "0.9588\n",
      "0.9549\n",
      "0.9574\n",
      "0.9555\n",
      "0.9639\n",
      "0.9597\n",
      "0.9636\n",
      "0.966\n",
      "0.9586\n",
      "0.9606\n",
      "0.97\n",
      "0.9687\n",
      "0.961\n",
      "0.9674\n",
      "0.9667\n",
      "0.9644\n",
      "0.9539\n",
      "0.962\n",
      "0.96\n",
      "0.9566\n",
      "0.9675\n",
      "0.9693\n",
      "0.975\n",
      "0.964\n",
      "0.9616\n",
      "0.9635\n",
      "0.9643\n",
      "0.96\n",
      "0.9613\n",
      "0.9701\n",
      "0.9671\n",
      "0.9643\n",
      "0.9712\n",
      "0.9661\n",
      "0.9644\n",
      "0.9704\n",
      "0.9648\n",
      "0.9592\n",
      "0.9721\n",
      "0.963\n",
      "0.9748\n",
      "0.96\n",
      "0.9638\n",
      "0.9728\n",
      "0.9672\n",
      "0.9686\n",
      "0.9672\n",
      "0.9663\n",
      "0.9606\n",
      "0.9631\n",
      "0.9642\n",
      "0.9652\n",
      "0.9714\n",
      "0.9704\n",
      "0.9745\n",
      "0.9679\n",
      "0.9661\n",
      "0.9709\n",
      "0.9758\n",
      "0.9734\n",
      "0.9704\n",
      "0.9711\n",
      "0.9739\n",
      "0.966\n",
      "0.9749\n",
      "0.9757\n",
      "0.9734\n",
      "0.9686\n",
      "0.9739\n",
      "0.9719\n",
      "0.9677\n",
      "0.9646\n",
      "0.971\n",
      "0.9724\n",
      "0.9724\n",
      "0.9608\n",
      "0.9714\n",
      "0.9681\n",
      "0.9698\n",
      "0.9685\n",
      "0.9698\n",
      "0.9696\n",
      "0.9629\n",
      "0.9717\n",
      "0.9654\n",
      "0.9612\n",
      "0.9727\n",
      "0.974\n",
      "0.9666\n",
      "0.9695\n",
      "0.9585\n",
      "0.9745\n",
      "0.9647\n",
      "0.9753\n",
      "0.9691\n",
      "0.9664\n",
      "0.9714\n",
      "0.9676\n",
      "0.9706\n",
      "0.9658\n",
      "0.9731\n",
      "0.9728\n",
      "0.9728\n",
      "0.9752\n",
      "0.9751\n",
      "0.9695\n",
      "0.9713\n",
      "0.9743\n",
      "0.9764\n",
      "0.9742\n",
      "0.9753\n",
      "0.9713\n",
      "0.97\n",
      "0.9734\n",
      "0.9664\n",
      "0.9742\n",
      "0.9694\n",
      "0.9696\n",
      "0.9739\n",
      "0.971\n",
      "0.9778\n",
      "0.9745\n",
      "0.9737\n",
      "0.9624\n",
      "0.9732\n",
      "0.975\n",
      "0.9737\n",
      "0.9708\n",
      "0.9685\n",
      "0.9604\n",
      "0.9648\n",
      "0.9794\n",
      "0.9764\n",
      "0.9748\n",
      "0.9747\n",
      "0.9717\n",
      "0.9772\n",
      "0.9719\n",
      "0.9616\n",
      "0.9738\n",
      "0.9694\n",
      "0.9639\n",
      "0.9712\n",
      "0.9769\n",
      "0.9607\n",
      "0.9663\n",
      "0.9714\n",
      "0.9731\n",
      "0.9664\n",
      "0.9737\n",
      "0.9754\n",
      "0.9742\n",
      "0.9721\n",
      "0.9609\n",
      "0.9664\n",
      "0.9774\n",
      "0.9728\n",
      "0.9744\n",
      "0.9748\n",
      "0.9737\n",
      "0.9773\n",
      "0.9688\n",
      "0.9783\n",
      "0.9774\n",
      "0.9797\n",
      "0.9731\n",
      "0.9762\n",
      "0.9774\n",
      "0.9747\n",
      "0.9696\n",
      "0.976\n",
      "0.9773\n",
      "0.9774\n",
      "0.9757\n",
      "0.9769\n",
      "0.9792\n",
      "0.9725\n",
      "0.9736\n",
      "0.9757\n",
      "0.978\n",
      "0.9789\n",
      "0.9709\n",
      "0.9762\n",
      "0.979\n",
      "0.9774\n",
      "0.9675\n",
      "0.9729\n",
      "0.9774\n",
      "0.9786\n",
      "0.9791\n",
      "0.9672\n",
      "0.9742\n",
      "0.9736\n",
      "0.9804\n",
      "0.9777\n",
      "0.974\n",
      "0.9791\n",
      "0.9771\n",
      "0.9756\n",
      "0.9774\n",
      "0.9777\n",
      "0.9749\n",
      "0.9754\n",
      "0.9779\n",
      "0.9726\n",
      "0.98\n",
      "0.9754\n",
      "0.9759\n",
      "0.9738\n",
      "0.9755\n",
      "0.9794\n",
      "0.9744\n",
      "0.9769\n",
      "0.9759\n",
      "0.9757\n",
      "0.9715\n",
      "0.9749\n",
      "0.9729\n",
      "0.9732\n",
      "0.9736\n",
      "0.9734\n",
      "0.9767\n",
      "0.9771\n",
      "0.9742\n",
      "0.9759\n",
      "0.9739\n",
      "0.9774\n",
      "0.9744\n",
      "0.98\n",
      "0.9741\n",
      "0.9726\n",
      "0.9772\n",
      "0.9756\n",
      "0.974\n",
      "0.9725\n",
      "0.9773\n",
      "0.9767\n",
      "0.9768\n",
      "0.9771\n",
      "0.975\n",
      "0.9793\n",
      "0.9767\n",
      "0.9761\n",
      "0.977\n",
      "0.9779\n",
      "0.9768\n",
      "0.9757\n",
      "0.9772\n",
      "0.9797\n",
      "0.9745\n",
      "0.9779\n",
      "0.9775\n",
      "0.9784\n",
      "0.9778\n",
      "0.9775\n",
      "0.9768\n",
      "0.9773\n",
      "0.9807\n",
      "0.9779\n",
      "0.976\n",
      "0.9695\n",
      "0.9789\n",
      "0.9747\n",
      "0.9717\n",
      "0.9797\n",
      "0.9763\n",
      "0.9766\n",
      "0.9762\n",
      "0.9784\n",
      "0.9752\n",
      "0.9767\n",
      "0.9766\n",
      "0.9803\n",
      "0.9777\n",
      "0.98\n",
      "0.9792\n",
      "0.9767\n",
      "0.9721\n",
      "0.9767\n",
      "0.9775\n",
      "0.9767\n",
      "0.9789\n",
      "0.981\n",
      "0.9754\n",
      "0.9791\n",
      "0.98\n",
      "0.9768\n",
      "0.9779\n",
      "0.9787\n",
      "0.9793\n",
      "0.9812\n",
      "0.9761\n",
      "0.9716\n",
      "0.9776\n",
      "0.9765\n",
      "0.9776\n",
      "0.9771\n",
      "0.9795\n",
      "0.9768\n",
      "0.9777\n",
      "0.9744\n",
      "0.9738\n",
      "0.9804\n",
      "0.9789\n",
      "0.9775\n",
      "0.9778\n",
      "0.9741\n",
      "0.9787\n",
      "0.9652\n",
      "0.9768\n",
      "0.9778\n",
      "0.9756\n",
      "0.9765\n",
      "0.979\n",
      "0.9775\n",
      "0.9777\n",
      "0.9775\n",
      "0.9799\n",
      "0.9765\n",
      "0.9796\n",
      "0.9773\n",
      "0.9797\n",
      "0.9797\n",
      "0.9773\n",
      "0.9805\n",
      "0.9801\n",
      "0.9774\n",
      "0.98\n",
      "0.981\n",
      "0.9778\n",
      "0.9812\n",
      "0.974\n",
      "0.9794\n",
      "0.9774\n",
      "0.9787\n",
      "0.9789\n",
      "0.9774\n",
      "0.9746\n"
     ]
    }
   ],
   "source": [
    "# Train\n",
    "for i in range(12000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
    "    if (i%30 ==0):\n",
    "      correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "      accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "      print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                      y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9778\n",
      "0.9798\n",
      "0.9794\n",
      "0.9772\n",
      "0.9779\n",
      "0.9823\n",
      "0.979\n",
      "0.9797\n",
      "0.9787\n",
      "0.9734\n",
      "0.9753\n",
      "0.9791\n",
      "0.9801\n",
      "0.9762\n",
      "0.9739\n",
      "0.9778\n",
      "0.9774\n",
      "0.9733\n",
      "0.9765\n",
      "0.9786\n",
      "0.9789\n",
      "0.9765\n",
      "0.9785\n",
      "0.9763\n",
      "0.9775\n",
      "0.9736\n",
      "0.9796\n",
      "0.9762\n",
      "0.9791\n",
      "0.9797\n",
      "0.9816\n",
      "0.9809\n",
      "0.9781\n",
      "0.9784\n",
      "0.9773\n",
      "0.9759\n",
      "0.9809\n",
      "0.9791\n",
      "0.9769\n",
      "0.9795\n",
      "0.9796\n",
      "0.98\n",
      "0.9784\n",
      "0.9759\n",
      "0.9729\n",
      "0.9819\n",
      "0.9795\n",
      "0.9798\n",
      "0.9799\n",
      "0.9793\n",
      "0.9817\n",
      "0.9796\n",
      "0.9823\n",
      "0.983\n",
      "0.9787\n",
      "0.9798\n",
      "0.9802\n",
      "0.9793\n",
      "0.9793\n",
      "0.9739\n",
      "0.9782\n",
      "0.9799\n",
      "0.977\n",
      "0.9775\n",
      "0.9787\n",
      "0.9791\n",
      "0.9693\n",
      "0.9762\n",
      "0.978\n",
      "0.9789\n",
      "0.9796\n",
      "0.9821\n",
      "0.9796\n",
      "0.9795\n",
      "0.98\n",
      "0.9803\n",
      "0.9801\n",
      "0.9777\n",
      "0.978\n",
      "0.9815\n",
      "0.9787\n",
      "0.979\n",
      "0.978\n",
      "0.9799\n",
      "0.9755\n",
      "0.9772\n",
      "0.9805\n",
      "0.9809\n",
      "0.9817\n",
      "0.9771\n",
      "0.9796\n",
      "0.9787\n",
      "0.9807\n",
      "0.9741\n",
      "0.9799\n",
      "0.9769\n",
      "0.9777\n",
      "0.9791\n",
      "0.979\n",
      "0.9806\n",
      "0.9774\n",
      "0.9796\n",
      "0.9769\n",
      "0.9796\n",
      "0.9782\n",
      "0.9818\n",
      "0.9809\n",
      "0.9785\n",
      "0.9791\n",
      "0.9793\n",
      "0.9782\n",
      "0.9779\n",
      "0.9794\n",
      "0.979\n",
      "0.975\n",
      "0.9751\n",
      "0.9782\n",
      "0.9777\n",
      "0.9761\n",
      "0.9778\n",
      "0.9802\n",
      "0.9798\n",
      "0.9762\n",
      "0.972\n",
      "0.9792\n",
      "0.9766\n",
      "0.9802\n",
      "0.9802\n",
      "0.9787\n",
      "0.9804\n",
      "0.9796\n",
      "0.9787\n",
      "0.9791\n",
      "0.978\n",
      "0.9783\n",
      "0.9786\n",
      "0.9807\n",
      "0.977\n",
      "0.9739\n",
      "0.9784\n",
      "0.9781\n",
      "0.9782\n",
      "0.9785\n",
      "0.9779\n",
      "0.9809\n",
      "0.9797\n",
      "0.9762\n",
      "0.9814\n",
      "0.9817\n",
      "0.9811\n",
      "0.9778\n",
      "0.9768\n",
      "0.9802\n",
      "0.9801\n",
      "0.9766\n",
      "0.9807\n",
      "0.9769\n",
      "0.9802\n",
      "0.9788\n",
      "0.973\n",
      "0.9799\n",
      "0.9789\n",
      "0.9802\n",
      "0.9808\n",
      "0.9768\n",
      "0.9779\n",
      "0.9798\n",
      "0.9799\n",
      "0.9783\n",
      "0.9764\n",
      "0.9766\n",
      "0.9779\n",
      "0.9756\n",
      "0.9801\n",
      "0.9722\n",
      "0.9787\n",
      "0.9789\n",
      "0.9785\n",
      "0.9791\n",
      "0.9799\n",
      "0.9807\n",
      "0.9811\n",
      "0.9775\n",
      "0.98\n",
      "0.9804\n",
      "0.9783\n",
      "0.9811\n",
      "0.9787\n",
      "0.9781\n",
      "0.9782\n",
      "0.9798\n",
      "0.9778\n",
      "0.9777\n",
      "0.9758\n",
      "0.9806\n",
      "0.9794\n",
      "0.9794\n",
      "0.9813\n",
      "0.981\n",
      "0.9804\n",
      "0.98\n",
      "0.9822\n",
      "0.9798\n",
      "0.9795\n",
      "0.9817\n",
      "0.9818\n",
      "0.9787\n",
      "0.98\n",
      "0.9793\n",
      "0.9764\n",
      "0.9788\n",
      "0.9788\n",
      "0.9788\n",
      "0.9758\n",
      "0.9801\n",
      "0.978\n",
      "0.9783\n",
      "0.979\n",
      "0.9788\n",
      "0.9791\n",
      "0.9802\n",
      "0.9783\n",
      "0.9815\n",
      "0.9786\n",
      "0.9775\n",
      "0.9777\n",
      "0.9758\n",
      "0.9774\n",
      "0.978\n",
      "0.9771\n",
      "0.9773\n",
      "0.9793\n",
      "0.9798\n",
      "0.9806\n",
      "0.9814\n",
      "0.9811\n",
      "0.9809\n",
      "0.9787\n",
      "0.9741\n",
      "0.9795\n",
      "0.9786\n",
      "0.9797\n",
      "0.9816\n",
      "0.9785\n",
      "0.9808\n",
      "0.9813\n",
      "0.9785\n",
      "0.9801\n",
      "0.9795\n",
      "0.9767\n",
      "0.9766\n",
      "0.9746\n",
      "0.9797\n",
      "0.9791\n",
      "0.9799\n",
      "0.9773\n",
      "0.9797\n",
      "0.9811\n",
      "0.9791\n",
      "0.9793\n",
      "0.9806\n",
      "0.9755\n",
      "0.9802\n",
      "0.9769\n",
      "0.9792\n",
      "0.98\n",
      "0.98\n",
      "0.9809\n",
      "0.9795\n",
      "0.9803\n",
      "0.9792\n",
      "0.9798\n",
      "0.9799\n",
      "0.9782\n",
      "0.9774\n",
      "0.9769\n",
      "0.9788\n",
      "0.9781\n",
      "0.9788\n",
      "0.9781\n",
      "0.9775\n",
      "0.9791\n",
      "0.9784\n",
      "0.9801\n",
      "0.9797\n",
      "0.9818\n",
      "0.9808\n",
      "0.9786\n",
      "0.9774\n",
      "0.9806\n",
      "0.9774\n",
      "0.9801\n",
      "0.9798\n",
      "0.9817\n",
      "0.981\n",
      "0.9808\n",
      "0.9772\n",
      "0.9775\n",
      "0.9782\n",
      "0.9791\n",
      "0.98\n",
      "0.9783\n",
      "0.9755\n",
      "0.9778\n",
      "0.9798\n",
      "0.982\n",
      "0.9789\n",
      "0.9815\n",
      "0.9812\n",
      "0.9827\n",
      "0.9818\n",
      "0.9807\n",
      "0.9814\n",
      "0.9816\n",
      "0.9818\n",
      "0.9809\n",
      "0.9793\n",
      "0.9814\n",
      "0.9801\n",
      "0.9797\n",
      "0.9783\n",
      "0.9812\n",
      "0.9797\n",
      "0.9812\n",
      "0.9799\n",
      "0.9806\n",
      "0.9814\n",
      "0.9812\n",
      "0.9819\n",
      "0.9821\n",
      "0.9809\n",
      "0.9814\n",
      "0.9791\n",
      "0.9785\n",
      "0.9773\n",
      "0.9782\n",
      "0.9812\n",
      "0.9801\n",
      "0.9814\n",
      "0.9765\n",
      "0.9806\n",
      "0.9801\n",
      "0.9799\n",
      "0.9805\n",
      "0.9805\n",
      "0.9804\n",
      "0.9807\n",
      "0.9796\n",
      "0.981\n",
      "0.979\n",
      "0.9799\n",
      "0.9793\n",
      "0.9799\n",
      "0.9777\n",
      "0.9802\n",
      "0.9797\n",
      "0.9764\n",
      "0.9792\n",
      "0.9777\n",
      "0.9789\n",
      "0.9809\n",
      "0.9819\n",
      "0.977\n",
      "0.9809\n",
      "0.98\n",
      "0.9793\n",
      "0.9814\n",
      "0.9792\n",
      "0.9792\n",
      "0.9796\n",
      "0.9782\n",
      "0.976\n",
      "0.9808\n",
      "0.9796\n",
      "0.9789\n",
      "0.9794\n",
      "0.9802\n",
      "0.9815\n",
      "0.9813\n",
      "0.9782\n",
      "0.9789\n",
      "0.982\n",
      "0.9815\n",
      "0.9811\n",
      "0.9813\n",
      "0.9797\n",
      "0.9794\n",
      "0.9791\n",
      "0.9803\n",
      "0.9801\n",
      "0.9807\n",
      "0.9784\n",
      "0.9776\n",
      "0.9772\n",
      "0.9772\n",
      "0.9787\n",
      "0.9778\n",
      "0.9794\n",
      "0.9798\n",
      "0.9797\n"
     ]
    }
   ],
   "source": [
    "#再训练12000次，使正确率稳定在0.98\n",
    "for i in range(12000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
    "    if (i%30 ==0):\n",
    "      correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "      accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "      print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                      y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9807\n",
      "0.9814\n",
      "0.9831\n"
     ]
    }
   ],
   "source": [
    "#使正确率稳定在0.98\n",
    "for i in range(90):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
    "    if (i%30 ==0):\n",
    "      correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "      accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "      print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                      y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "验证我们模型在测试数据上的准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 450,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy is:\n",
      "0.9813\n"
     ]
    }
   ],
   "source": [
    "  # Test trained model\n",
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "print(\"accuracy is:\")\n",
    "print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
    "                                      y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到经过 这些超参数的调整，经过12000次调整，使得准确率稳定在 0.98"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "毫无疑问，这个模型是一个非常简陋，性能也不理想的模型。目前只能达到92%左右的准确率。\n",
    "接下来，希望大家利用现有的知识，将这个模型优化至98%以上的准确率。\n",
    "Hint：\n",
    "- 多隐层\n",
    "- 激活函数\n",
    "- 正则化\n",
    "- 初始化\n",
    "- 摸索一下各个超参数\n",
    "  - 隐层神经元数量\n",
    "  - 学习率\n",
    "  - 正则化惩罚因子\n",
    "  - 最好每隔几个step就对loss、accuracy等等进行一次输出，这样才能有根据地进行调整"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
